"ui";
var appName = "碧蓝航线脚本";
ui.statusBarColor("#FF4FB3FF")
ui.layout(
    <drawer id="drawer">
        <vertical>
        <appbar>
                <toolbar id="toolbar" bg="#ff4fb3ff" title="{{appName}}"/>
            </appbar>
            <vertical gravity="center" layout_weight="1">
                <frame>
                <viewpager h="84" id="pager">
                <vertical>
                <card w="*" h="68" margin="10 8" cardCornerRadius="6dp"
                        cardElevation="2dp" gravity="center">  
                    <linear>
                        <vertical margin="10" layout_gravity="center_vertical" layout_weight="1"> 
                            <text id="name" size="18" color="#444444" text="使用教程"/>
                            <text id="integral"  padding="1" size="16" text="..." foreground="?selectableItemBackground"/>
                        </vertical>
                        <button id="login" w="90" text="点击查看" color="#ff5d85ff" style="Widget.AppCompat.Button.Borderless.Colored"/>
                    </linear>                        
                </card>
                </vertical>
                <vertical>
                <card w="*" h="68" margin="10 8" cardCornerRadius="6dp"
                        cardElevation="2dp" gravity="center">  
                    <linear>
                        <vertical margin="10" layout_gravity="center_vertical" layout_weight="1"> 
                        <text id="imei" textIsSelectable="true" size="18" color="#444444" text="大更新"/>
                        <text id="Etime" size="16" text="在做了（0%）"/>
                        </vertical>
                        <button id="recharge" w="70" text="欠着" color="#ff5d85ff" style="Widget.AppCompat.Button.Borderless.Colored"/>
                    </linear>                        
                </card>
                </vertical>
                </viewpager>
                </frame>
                <tabs w="40" id="tabs" tabIndicatorColor="#777777" bg="#cfcfcf"  h="2"/>  
                <vertical padding="10 6 0 6" bg="#ffffff" w="*" h="auto" margin="0 5" elevation="1dp">
                    <Switch id="autoService" w="*" checked="{{auto.service != null}}" textColor="#666666" text="无障碍服务"/>   
                    <View h="5"/>  
                    <Switch id="floatytest" w="*" checked="{{floaty.checkPermission() != false}}" textColor="#666666" text="悬浮窗权限"/>      
                    
                </vertical> 
                        
                <vertical margin="0 5" bg="#ffffff" elevation="1dp" padding="5 5 10 5" w="*" h="auto">          
                    <linear>
                        <text text="功能（欠着）"/>   
                    </linear>
                    <linear>
                        <checkbox id="plcheck" text="开启双倍" layout_weight="1"/> 
                        <text gravity="center" id="DYlimit"/>
                    </linear>
                    <View h="5"/>
                                                     
                </vertical>  

                <vertical margin="0 5" bg="#ffffff" elevation="1dp" padding="5 5 10 5" w="*" h="auto">          
                    <linear>
                        <text text="活动"/>   
                    </linear>
                    <linear>
                        <checkbox id="activities" text="穿越水线" layout_weight="1"/> 
                        <text gravity="center" id="DYlimit"/>
                    </linear>
                    <View h="5"/>
                                                     
                </vertical>  

                <linear>
                    <text layout_weight="1" size="19" color="#222222" text="日志"/>
                    <button id="tolog" h="40" text="全部日志" style="Widget.AppCompat.Button.Borderless.Colored"/> 
                </linear>
                <text paddingLeft="5" size="16" id="oneLog"/>
                
                <list bg="#ffffff" elevation="1dp" h="*" id="logList">
                    <linear>
                    <text size="13" textColor="#555555" text="{{time}} "/>
                    <text size="13" text="{{message}}"/>
                    </linear>
                </list>       
            </vertical>
            <button id="start" text="开始运行" tag="ScriptTag" color="#ffffff" bg="#FF4FB3FF" foreground="?selectableItemBackground"/>
        </vertical>
   </drawer>
);

//创建选项菜单(右上角)
ui.emitter.on("create_options_menu", menu => {
    menu.add("设置");
    menu.add("热更新");
    menu.add("关于");
    menu.add("检测更新");
});
//监听选项菜单点击
ui.emitter.on("options_item_selected", (e, item) => {
    switch (item.getTitle()) {
        case "设置":
            toast("还没有设置");
            break;
        case "热更新":
            toast("开始热更新");
            var lists = ["blhx.js","running.js","bdocr.js","穿越水线.js","main.js"];
            threads.start(function(){
                lists.forEach(item => {
                    var path = "/" + item;
                    var url = "http://www.chorblack.top/js/" + item;
                    var r = http.get(url, {
                        headers: {
                            'Accept-Language': 'zh-cn,zh;q=0.5',
                            'User-Agent': 'Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11'
                        }
                    });
                    var res = r.body.string();
                    files.write(engines.myEngine().cwd()+path,res);
                });
                alert("热更新完成,请重启应用！");
            });
            break;

        case "关于":
            alert("关于", "碧蓝航线脚本(⑉°з°)-♡");
            break;
        case "检测更新":
            var r = http.get("http://1.116.230.176:8001/lastest.txt");
            var lastest = r.body.string();
            if(app.versionCode == lastest){
                toast("已经是最新版啦");
            }else{
                dialogs.build({
                    //对话框标题
                    title: "发现新版本",
                    //对话框内容
                    content: "更新日志: 我不知道",
                    //确定键内容
                    positive: "下载",
                    //取消键内容
                    negative: "取消",
                    //中性键内容
                    neutral: "到浏览器下载",
                }).on("positive", ()=>{
                    //监听确定键
                    toast("开始下载....");
                    threads.start(function(){
                        var path = "/sdcard/碧蓝航线脚本/blhx.apk";
                        files.createWithDirs(path);
                        var r = http.get("http://1.116.230.176:8000/onedrive1/blhxscript/blhx.apk");
                        files.writeBytes(path, r.body.bytes());
                        app.viewFile(path);
                    });
                }).on("neutral", ()=>{
                    //监听中性键
                    app.openUrl("http://1.116.230.176:8000/onedrive1/blhxscript/blhx.apk?preview");
                }).show();
                threads.shutDownAll();                
            }
            
    }
    e.consumed = true;
});
activity.setSupportActionBar(ui.toolbar);

//设置滑动模式
ui.logList.setOverScrollMode(2);
//设置滑动页面的标题
ui.pager.setTitles(["",""]); 
//让滑动页面和标签栏联动
ui.tabs.setupWithViewPager(ui.pager);

//无障碍开关监控
ui.autoService.setOnCheckedChangeListener(function(widget,checked) {
    if(checked&&!auto.service) {     
        app.startActivity({
            action: "android.settings.ACCESSIBILITY_SETTINGS"
        });
    }
    
});

ui.floatytest.setOnCheckedChangeListener(function(checked) {
    if(checked) {     
        app.startActivity({
            packageName: "com.android.settings",
            className: "com.android.settings.Settings$AppDrawOverlaySettingsActivity",
            data: "package:" + context.getPackageName(),
        });        
    }
});


ui.tolog.click(()=>{
   app.startActivity("console") 
})


var execution = null;
ui.start.click(()=>{
    if(ui.start.getText() == '开始运行'){
        execution = engines.execScriptFile("./floaty.js");
        ui.start.setText('停止运行');
    }else{
        if(execution){
            execution.getEngine().forceStop();
        }
        ui.start.setText('开始运行');
    }
});
/*
ui.start.on("long_click", ()=>{
    toast("我被长按啦");
});
*/
ui.login.on("click", ()=>{
    engines.execScriptFile("./help.js");
});

var storage = storages.create("config");
storage.put("高效作战", "false");
ui.plcheck.on("check", (checked)=>{
    //监听勾选框 
    if(checked){
        storage.put("高效作战", true);
        log("高效作战 = " + storage.get("高效作战"));
        //storage.clear();
    }else{
        storage.put("高效作战", false);
        log("高效作战 = " + storage.get("高效作战"));
        //storage.clear();
    }
});

ui.activities.on("check", (checked)=>{
    //监听勾选框 
    if(checked){
        storage.put("穿越水线", true);
        log("穿越水线 = " + storage.get("穿越水线"));
        //storage.clear();
    }else{
        storage.put("穿越水线", false);
        log("穿越水线 = " + storage.get("穿越水线"));
        //storage.clear();
    }
});
